package parser

import (
	"qweewq.tk/example/crawier/engine"
	"regexp"
)

const zhenaiCityUserReg = `<div class="g-list" data-v-[0-9a-z]+>(.+)<div class="f-pager"><ul class="m-page">(.+)</ul></div></div>`
const zhenaiCityNextPage = `.*<a href="(.*?)">下一页</a>.*?</li>`

func ParserCityPageItem(content []byte) *engine.ParserResult {
	//解析页面数据
	reg := regexp.MustCompile(zhenaiCityNextPage)
	matches := reg.FindAllSubmatch(content, -1)

	var requests []engine.Request
	var items []engine.Item

	for _, elem := range matches {
		requests = append(requests, engine.Request{
			Url:        string(elem[1]),
			ParserFunc: ParserCityUser,
		})
	}
	return &engine.ParserResult{
		Requests: requests,
		Items:    items,
	}
}

func ParserCityUser(content []byte) *engine.ParserResult {
	//使用正则表达式匹配用户数据项
	reg := regexp.MustCompile(zhenaiCityUserReg)
	matches := reg.FindAllSubmatch(content, -1)

	var requests []engine.Request
	var items []engine.Item

	for _, elem := range matches {
		userItemResult := ParserUserItemGroup(elem[1])
		pageItemResult := ParserCityPageItem(elem[2])

		if userItemResult != nil {
			requests = append(requests, userItemResult.Requests...)
			items = append(items, userItemResult.Items)
		}

		if pageItemResult != nil {
			requests = append(requests, pageItemResult.Requests...)
		}

	}
	return &engine.ParserResult{
		Requests: requests,
		Items:    items,
	}
}
